package com.tbit.uqbike.object.view.sugar;

import com.tbit.uqbike.object.pojo.AlarmDayCount;
import com.tbit.uqbike.object.view.NameObject;
import org.springframework.web.servlet.support.RequestContext;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author MyWin E-mail:335918956@qq.com
 * @version 1.0
 * @createTime 2020-01-06 19:16
 * 工具类
 */
public class SugarHelper {
    /**
     * 转换成饼状图
     *
     * @param nos
     * @return
     */
    public static List<SugarPieItem> buildPies(List<NameObject> nos, RequestContext context) {
        List<SugarPieItem> list = new LinkedList<>();
        if (null != nos) {
            for (NameObject no : nos) {
                Integer v = new Long((Long) no.getValue()).intValue();
                Integer iv = 0;
                if (v != null && v instanceof Integer) {
                    iv = (Integer) v;
                }
                list.add(new SugarPieItem(context.getMessage(String.format("sugar.%s", no.getName())), iv));
            }
        }
        return list;
    }

    public static List<SugarPieItem> buildPies(Map<String, Object> map, RequestContext context) {
        List<SugarPieItem> list = new LinkedList<>();
        if (null != map) {
            for (Map.Entry<String, Object> kv : map.entrySet()) {
                Object v = kv.getValue();
                if (v instanceof Integer) {
                    list.add(new SugarPieItem(context.getMessage(String.format("sugar.%s", kv.getKey())), (Integer) kv.getValue()));
                } else if (v instanceof Double) {
                    list.add(new SugarPieItem(context.getMessage(String.format("sugar.%s", kv.getKey())), (Double) kv.getValue()));
                }
            }
        }
        return list;
    }

    public static List<SugarPieItem> buildPiesInt(Map<String, Integer> map, RequestContext context) {
        List<SugarPieItem> list = new LinkedList<>();
        if (null != map) {
            for (Map.Entry<String, Integer> kv : map.entrySet()) {
                list.add(new SugarPieItem(context.getMessage(kv.getKey()), kv.getValue()));
            }
        }
        return list;
    }

    public static List<SugarCoreItem> buildCoreItems(List<SugarCoreItem> scis, RequestContext context) {
        if (null != scis) {
            for (SugarCoreItem item : scis) {
                if (null != item.name) {
                    item.name = context.getMessage(item.name);
                }
                if (null != item.rate_tip) {
                    item.rate_tip = context.getMessage(item.rate_tip);
                }
            }
        }
        return scis;
    }

    public static List<SugarCoreItem> buildCoreMoneyItems(CoreMoney cm) {
        List<SugarCoreItem> list = new LinkedList<>();
        if (null != cm) {
            SugarCoreItem item = new SugarCoreItem("currDayMoney", cm.currDayMoney);
            item.rate_tip = String.format("%sTip", item.name);
            item.rate_level = "green";
            list.add(item);
            item = new SugarCoreItem("currWeekMoney", cm.currWeekMoney);
            item.rate_tip = String.format("%sTip", item.name);
            item.rate_level = "custom";
            item.color = "#f05050";
            list.add(item);
            item = new SugarCoreItem("lastWeekMoney", cm.lastWeekMoney);
            item.rate_tip = String.format("%sTip", item.name);
            list.add(item);
            item = new SugarCoreItem("yesterDayMoney", cm.yesterDayMoney);
            item.rate_tip = String.format("%sTip", item.name);
            list.add(item);
        }
        return list;
    }

    public static List<SugarCoreItem> buildCoreAlarmItems(Map<Integer, Integer> map) {
        List<Integer> alarmCodeList = new LinkedList() {
            {
                add(3);
                add(5);
                add(8);
            }
        };
        List<SugarCoreItem> list = new LinkedList<>();
        for (Integer code : alarmCodeList) {
            if (!map.containsKey(code)) {
                map.put(code, 0);
            }
            SugarCoreItem item = new SugarCoreItem(String.format("alarm.%d", code), map.get(code));
            item.rate_tip = String.format("%s.Tip", item.name);
            list.add(item);
        }
        return list;
    }

    public static HistogramData buildHistogram(String name, Map<String, Double> map, RequestContext context) {
        HistogramData data = new HistogramData();
        HistogramData.SeriesItem si = new HistogramData.SeriesItem();
        si.name = context.getMessage(String.format("sugar.%s", name));
        data.series.add(si);
        for (Map.Entry<String, Double> kv : map.entrySet()) {
            data.categories.add(kv.getKey());
            si.data.add(kv.getValue());
        }
        return data;
    }

    public static Map<String, Map<Integer, Integer>> initAlarmMap(List<AlarmDayCount> alarmCntList) {
        Map<String, Map<Integer, Integer>> map = new HashMap<>();
        for (AlarmDayCount item : alarmCntList) {
            if (!map.containsKey(item.getDt())) {
                map.put(item.getDt(), new HashMap<>());
            }
            map.get(item.getDt()).put(item.getAlarmType(), item.getCount());
        }
        return map;
    }


}
